/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.gfx.gui;

import rpg.gfx.Bitmap;

/**
 * rothens.tarhely.biz
 *
 * @author Rothens
 */
public class Bar extends Bitmap {

    int maxVal;
    int val;
    int color;
    int bgcolor;

    public Bar(int width, int height, int color, int bgcolor, int maxVal, int val) {
        super(width, height);
        this.color = (0xff << 24) | color;
        this.bgcolor = 0x000000;




        this.maxVal = maxVal;
        this.val = val;
        for (int i = 0; i < width * height; i++) {
            pixels[i] = this.bgcolor;
        }
        draw(calculateBar(), 1, 1);

    }

    public void setVal(int val) {
        if (val < 0) {
            val = 0;
        }
        if (val > maxVal) {
            val = maxVal;
        }

        this.val = val;
        draw(calculateBar(), 1, 1);
    }

    private Bitmap calculateBar() {
        Bitmap ret = new Bitmap(width - 2, height - 2);
        for (int i = 0; i < ret.width * ret.height; i++) {
            ret.pixels[i] = 0xff000001;
        }

        double percent = val / (double) maxVal;


        int w = (int) (ret.width * percent);
        Bitmap bar = new Bitmap(w, ret.height);
        for (int i = 0; i < bar.width * bar.height; i++) {
            bar.pixels[i] = color;
        }
        ret.draw(bar, 0, 0);
        return ret;
    }

    public void setColor(int color) {
        this.color = color;
        val = maxVal;
        draw(calculateBar(), 1, 1);
    }
}
